$(function () {
    // 清除所有缓存查询条件
    sessionStorage.removeItem('userKeyWord');
    sessionStorage.removeItem('deviceKeyWord');

    const selectType = sessionStorage.getItem("selectCompanyType");

    if (selectType === 'user') {
        $('.page-header').html('可选择相关公司用户列表');
    } else {
        $('.page-header').html('可选择相关公司设备列表');
    }

    // 自动加载设备列表信息
    loadCompanyData();
})

// 渲染底部显示数据
function loadCompanyData() {
    let userCompanyIdList = sessionStorage.getItem("userCompanyIdList");
    let content = '';

    if (null !== userCompanyIdList && userCompanyIdList !== '') {
        $.each(userCompanyIdList.split('@'), function (i, item) {
            if (item.trim().length > 0) {
                content += '<div class="card" onclick="toChildPage(\'' + item.split('-')[0] + '\',' + item.split('-')[1] + ')">';
                content += '<div class="card-content">';
                content += '<div class="card-title">' + item.split('-')[0] + '</div>';
                content += '</div></div>';
            }
        });

        $('.mainContent').html(content);
    }
}

function toChildPage(selectCompanyName, selectCompanyId) {
    sessionStorage.setItem('selectCompanyName', selectCompanyName);
    sessionStorage.setItem('selectCompanyId', selectCompanyId);

    const selectType = sessionStorage.getItem("selectCompanyType");

    if (selectType === 'user') {
        window.location.href = 'wx/center/user';
    } else {
        window.location.href = 'wx/center/device';
    }
}